<?php
namespace app\modules\epi\controllers;

use Yii;
use yii\db\Expression;
use ac\controllers\AuthController;
use app\models\SendAuction;
use ac\component\exception\ErrMap;
use ac\component\tool\Tool;
use app\models\OperationLog;


class SendAuctionController extends AuthController
{
    /*送拍列表*/
    public function actionSendAuctionList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();

        $sendAuction = new SendAuction;
        $count = $sendAuction::find()->count();
        $sendAuctionList = $sendAuction::find()->with('member')->with('goods')
                        ->orderBy(['id' => SORT_DESC])
                        ->offset($pageSize*($currPage-1))->limit($pageSize)
                        ->asArray()->all();
        $res = array(
            'list' => $sendAuctionList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        // P($sendAuctionList);
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*添加送拍*/
    public function actionAddSendAuction()
    {
        if(Yii::$app->request->isPost){
            $sendAuction = new SendAuction;
            if($sendAuction->addSendAuction($this->post)){
                $backInfo = [0, '添加送拍成功'];//返回信息
            }else{
                if($sendAuction->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                    $exception = $sendAuction->getErrors();//异常信息
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }


    /*修改送拍*/
    public function actionModifySendAuction()
    {
        /*取出单个数据*/
        if(Yii::$app->request->isGet){
            // P($this->get);
            $id = isset($this->get['id'])?$this->get['id']:'';
            if($id){
                $sendAuction = SendAuction::find()
                            ->where('id = :id', [':id' => $id])
                            ->asArray()->one();
                // P($sendAuction);
                if(!is_null($sendAuction)){
                    // P($sendAuction);
                    return Tool::showRes([0, '获取成功'], $sendAuction);
                };
            }
        }


        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost){
            $id = isset($this->post['SendAuction']['id'])?intval($this->post['SendAuction']['id']):'';
            if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $sendAuction = new SendAuction;
                if($sendAuction->modifySendAuction($this->post, $id)){
                    $backInfo = [0, '修改送拍成功'];//返回信息
                }else{
                    if($sendAuction->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $sendAuction->getErrors();//异常信息
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        return Tool::showRes(ErrMap::getError(10005));
    }

    /*删除送拍*/
    public function actionDeleteSendAuction()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $sendAuction = new SendAuction;
            if($sendAuction->deleteAll('id = :id', [':id' => $id])){//删除成功
                $backInfo = [0, '删除送拍成功'];//返回信息
            }else{//删除失败
                $backInfo = ErrMap::getError(10013, '删除送拍失败');//返回信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), '') ) != 'boolean'){
                return $res;
            };

        }
        return Tool::showRes(ErrMap::getError(10005));
    }

}
